home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Jumpstart / Multimedia Microsoft Jumpstart Version 1.1a (Microsoft).BIN / develpmt / source / transblt / transblt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-21  |  9.9 KB  |  423 lines

  1. /*
  2.      (C) Copyright Microsoft Corp. 1991, 1992.  All rights reserved.
  3.  
  4.      You have a royalty-free right to use, modify, reproduce and 
  5.      distribute the Sample Files (and/or any modified version) in 
  6.      any way you find useful, provided that you agree that 
  7.      Microsoft has no warranty obligations or liability for any 
  8.      Sample Application Files which are modified. 
  9.      
  10.  Draws an images with transparent portions.  The transparent areas are a
  11.  color in the source image.  Demonstrates the 'old' way and the new MM way
  12.  to do transparent drawings.
  13.      
  14.  */
  15.  
  16. #include <windows.h>
  17. #include <mmsystem.h>
  18. #include "transblt.h"
  19. #include "tblt.h"
  20.  
  21. #define TESTLENGTH  100
  22.  
  23. VOID NEAR PASCAL transbltCommand ( HWND hwnd, WPARAM wParam, LPARAM lParam );
  24. BOOL FAR PASCAL _export AboutDlgProc ( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  25.  
  26. #define  DSNA  0x00220326L
  27.  
  28. HANDLE hInst;
  29.  
  30. char    gach[120];
  31. char    szMsg1[] = "L button for driver method of transparency";
  32. char    szMsg2[] = "R button for traditional method of transparency";
  33.  
  34. /* handles used for the various bitmaps */
  35.  
  36. HBITMAP hBitmap=NULL;
  37. HBITMAP hBitmapMask=NULL;
  38.  
  39. HBITMAP hOBitmapMask;
  40. HBITMAP hOBitmapImage;
  41.  
  42. COLORREF transColor=RGB(0xFF, 0x00, 0xFF);
  43.  
  44.  
  45. HDC hDC;                             /* handle to device context           */
  46.  
  47. BOOL    fMask=FALSE;
  48. BOOL    fScreen=FALSE;
  49. BOOL    bTrans;
  50. WORD    nBitmap=0;
  51.  
  52. WORD    dummy;
  53.  
  54. BITMAP Bitmap;                         /* bitmap structure                   */
  55.  
  56. /****************************************************************************
  57.  
  58.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  59.  
  60.     PURPOSE: calls initialization function, processes message loop
  61.  
  62. ****************************************************************************/
  63.  
  64. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  65. HANDLE hInstance;
  66. HANDLE hPrevInstance;
  67. LPSTR lpCmdLine;
  68. int nCmdShow;
  69. {
  70.     MSG msg;
  71.  
  72.     if (!hPrevInstance)
  73.     if (!InitApplication(hInstance))
  74.         return (FALSE);
  75.  
  76.     if (!InitInstance(hInstance, nCmdShow))
  77.         return (FALSE);
  78.  
  79.     while (GetMessage(&msg, NULL, NULL, NULL)) 
  80.     {
  81.     TranslateMessage(&msg);
  82.     DispatchMessage(&msg);
  83.     }
  84.     return (msg.wParam);
  85. }
  86.  
  87.  
  88. /****************************************************************************
  89.  
  90.     FUNCTION: InitApplication(HANDLE)
  91.  
  92.     PURPOSE: Initializes window data and registers window class
  93.  
  94. ****************************************************************************/
  95.  
  96. BOOL InitApplication(hInstance)
  97. HANDLE hInstance;
  98. {
  99.     WNDCLASS  wc;
  100.  
  101.     wc.style = CS_VREDRAW | CS_HREDRAW;
  102.     wc.lpfnWndProc = MainWndProc;
  103.     wc.cbClsExtra = 0;
  104.     wc.cbWndExtra = 0;
  105.     wc.hInstance = hInstance;
  106.     wc.hIcon = LoadIcon(hInstance, "appicon");
  107.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  108.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  109.     wc.lpszMenuName = "transblt";
  110.     wc.lpszClassName = "TransbltWClass";
  111.     
  112.  
  113.     return (RegisterClass(&wc));
  114. }
  115.  
  116.  
  117. /****************************************************************************
  118.  
  119.     FUNCTION:  InitInstance(HANDLE, int)
  120.  
  121.     PURPOSE:  Saves instance handle and creates main window
  122.  
  123. ****************************************************************************/
  124.  
  125. BOOL InitInstance(hInstance, nCmdShow)
  126.     HANDLE          hInstance;
  127.     int             nCmdShow;
  128. {
  129.     HWND            hwnd;
  130.  
  131.     hInst = hInstance;
  132.  
  133.     hwnd = CreateWindow(
  134.         "TransbltWClass",
  135.         "Transparent Bitmap Sample Application",
  136.         WS_OVERLAPPEDWINDOW,
  137.         CW_USEDEFAULT,
  138.         CW_USEDEFAULT,
  139.         CW_USEDEFAULT,
  140.         CW_USEDEFAULT,
  141.         NULL,
  142.         NULL,
  143.         hInstance,
  144.         NULL
  145.     );
  146.  
  147.     if (!hwnd)
  148.         return (FALSE);
  149.  
  150.     ShowWindow(hwnd, nCmdShow);
  151.     UpdateWindow(hwnd);
  152.     return (TRUE);
  153.  
  154. }
  155.  
  156. /****************************************************************************
  157.  
  158.     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  159.  
  160.     PURPOSE:  Processes messages
  161.  
  162.     MESSAGES:
  163.  
  164.         WM_CREATE      - create window and objects
  165.         WM_LBUTTONUP   - end selection, draw bitmap
  166.         WM_DESTROY     - destroy window
  167.  
  168. ****************************************************************************/
  169.  
  170. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  171. HWND hWnd;
  172. UINT message;
  173. WPARAM wParam;
  174. LPARAM lParam;
  175. {
  176.  
  177.     PAINTSTRUCT    ps;
  178.     TEXTMETRIC  tm;
  179.     WORD    i;
  180.  
  181.     switch (message) 
  182.     {
  183.  
  184.     case WM_CREATE:
  185.  
  186.             hDC = GetDC(hWnd);
  187.         
  188.         hBitmap = LoadBitmap(hInst, "Image");
  189.         GetObject(hBitmap, sizeof(BITMAP), (LPSTR) &Bitmap);
  190.             bTrans = GetDeviceCaps(hDC, CAPS1) & C1_TRANSPARENT;
  191.         nBitmap=0;
  192.  
  193.             ReleaseDC(hWnd, hDC);
  194.         hDC=NULL;
  195.         break;
  196.         
  197.  
  198.     case WM_PAINT:
  199.         BeginPaint(hWnd, &ps);
  200.  
  201.             GetTextMetrics(ps.hdc, &tm);
  202.  
  203.         lstrcpy(gach,szMsg1);
  204.         if(bTrans)
  205.         lstrcat(gach," (Is Supported)");
  206.         else
  207.         lstrcat(gach," (Not Supported)");
  208.         
  209.             TextOut(ps.hdc, 0, 0, gach, lstrlen(gach));
  210.             TextOut(ps.hdc, 0, tm.tmHeight, szMsg2, lstrlen(szMsg2));
  211.  
  212.         EndPaint(hWnd, &ps);
  213.         break;
  214.  
  215.  
  216.         case WM_LBUTTONDOWN:
  217.  
  218.             if (bTrans)
  219.         {
  220.         hDC = GetDC(hWnd);
  221.         
  222.         doTransBlit(hDC, LOWORD(lParam), HIWORD(lParam),
  223.             0,0, hBitmap, &Bitmap, 0, 0, NULL, transColor,
  224.             TRANS_NOSAVEDC | (fScreen ? TRANS_FORCESCREEN : 0));
  225.         
  226.         ReleaseDC(hWnd, hDC);
  227.         }
  228.             else 
  229.         {
  230.         MessageBeep(MB_ICONEXCLAMATION);
  231.         MessageBox(hWnd, (LPSTR)"Driver does not support transparency",
  232.                          (LPSTR)"GetDeviceCaps", MB_ICONEXCLAMATION);
  233.             }
  234.  
  235.  
  236.  
  237.             break;
  238.  
  239.     case WM_RBUTTONDOWN:
  240.  
  241.             hDC = GetDC(hWnd);
  242.         doTransBlit(hDC, LOWORD(lParam), HIWORD(lParam),
  243.         0,0, hBitmap, &Bitmap, 0, 0, &hBitmapMask, transColor,
  244.         TRANS_NOSAVEDC | TRANS_FORCEOLD | 
  245.         (fScreen ? TRANS_FORCESCREEN : 0) |
  246.         (fMask   ? TRANS_SAVEMASK    : 0) 
  247.         );
  248.             ReleaseDC(hWnd, hDC);
  249.             break;
  250.  
  251.     case WM_DESTROY:
  252.         DeleteObject(hBitmap);
  253.         if(hBitmapMask)
  254.         DeleteObject(hBitmapMask);
  255.             
  256.         PostQuitMessage(0);
  257.         break;
  258.         
  259.     case WM_COMMAND:
  260.         transbltCommand(hWnd, wParam, lParam);
  261.         break;
  262.         
  263.     case WM_INITMENU:
  264.         CheckMenuItem((HMENU)wParam, IDM_MASK, fMask ? MF_CHECKED : MF_UNCHECKED);
  265.         CheckMenuItem((HMENU)wParam, IDM_SCREEN, fScreen ? MF_CHECKED : MF_UNCHECKED);
  266.         for (i=IDM_DEFAULT;i<=IDM_BITMAP_LAST; i++)
  267.         CheckMenuItem((HMENU)wParam, i, MF_UNCHECKED);
  268.     
  269.         CheckMenuItem((HMENU)wParam, IDM_DEFAULT + nBitmap, MF_CHECKED);
  270.  
  271.         break;
  272.     
  273.  
  274.     default:
  275.         return (DefWindowProc(hWnd, message, wParam, lParam));
  276.     }
  277.     return (NULL);
  278. }
  279.  
  280.  
  281. VOID NEAR PASCAL transbltCommand ( HWND hwnd, WPARAM wParam, LPARAM lParam )
  282. {
  283.     DWORD   dwTransNew;
  284.     DWORD   dwTransOld;
  285.     HDC        hDC;
  286.     DWORD   endtime;
  287.     HCURSOR hcur;
  288.     int        i;
  289.     
  290.     switch (wParam)
  291.     {
  292.     case IDM_TEST:
  293.         hDC = GetDC(hwnd);
  294.         
  295.         hcur = SetCursor(LoadCursor(NULL,IDC_WAIT));
  296.  
  297.         dwTransOld=GetCurrentTime();
  298.         for (i=0;i<TESTLENGTH;i++)
  299.         {
  300.         doTransBlit(hDC, LOWORD(lParam), HIWORD(lParam),
  301.             0,0, hBitmap, &Bitmap, 0, 0, &hBitmapMask, transColor,
  302.             TRANS_NOSAVEDC | TRANS_FORCEOLD | 
  303.             (fScreen ? TRANS_FORCESCREEN : 0) |
  304.             (fMask   ? TRANS_SAVEMASK    : 0) 
  305.             );
  306.         }
  307.         if (bTrans)
  308.         {
  309.         dwTransNew=GetCurrentTime();
  310.         for (i=0;i<TESTLENGTH;i++)
  311.         {
  312.         
  313.             doTransBlit(hDC, LOWORD(lParam), HIWORD(lParam),
  314.             0,0, hBitmap, &Bitmap, 0, 0, NULL, transColor,
  315.             TRANS_NOSAVEDC | (fScreen ? TRANS_FORCESCREEN : 0));
  316.         }
  317.         }
  318.         endtime=GetCurrentTime();
  319.         SetCursor(hcur);
  320.             ReleaseDC(hwnd, hDC);
  321.  
  322.         if(bTrans)
  323.         {
  324.         wsprintf(gach,"%d iterations took \n\t%ld MS Old way\n\t"
  325.             "%ld MS New way",i,dwTransNew-dwTransOld, endtime-dwTransNew);
  326.         }
  327.         else
  328.         {
  329.         wsprintf(gach,"%d iterations took \n\t%ld MS Old way\n\t"
  330.             "Can't do new way.",i,endtime-dwTransOld);
  331.         }
  332.         MessageBox(hwnd, gach,"Timing",MB_APPLMODAL | MB_ICONINFORMATION| MB_OK);
  333.         break;
  334.     case IDM_CAT:
  335.         if(nBitmap== (wParam-IDM_DEFAULT))
  336.         break;
  337.         nBitmap=wParam-IDM_DEFAULT;
  338.         
  339.         DeleteObject(hBitmap);
  340.         if(hBitmapMask)        // mask bitmap
  341.         DeleteObject(hBitmapMask);
  342.  
  343.         hBitmapMask=NULL;
  344.         
  345.             hDC = GetDC(hwnd);
  346.         hBitmap = LoadBitmap(hInst, "Cat");
  347.         GetObject(hBitmap, sizeof(BITMAP), (LPSTR) &Bitmap);
  348.             ReleaseDC(hwnd, hDC);
  349.         hDC=NULL;
  350.         break;
  351.         
  352.     case IDM_DEFAULT:
  353.         if(nBitmap== (wParam-IDM_DEFAULT))
  354.         break;
  355.         
  356.         nBitmap=wParam-IDM_DEFAULT;
  357.         
  358.         DeleteObject(hBitmap);
  359.         if(hBitmapMask)        // mask bitmap
  360.         DeleteObject(hBitmapMask);
  361.  
  362.         hBitmapMask=NULL;
  363.         
  364.             hDC = GetDC(hwnd);
  365.         hBitmap = LoadBitmap(hInst, "Image");
  366.         GetObject(hBitmap, sizeof(BITMAP), (LPSTR) &Bitmap);
  367.             ReleaseDC(hwnd, hDC);
  368.         hDC=NULL;
  369.             break;
  370.  
  371.     case IDM_HELPABOUT:
  372.     {
  373.         // Bring up the ubiquitous Ego box
  374.         FARPROC lpfn;
  375.  
  376.         lpfn = MakeProcInstance(AboutDlgProc, hInst);
  377.         DialogBox (hInst, IDD_ABOUT, hwnd, lpfn);
  378.         FreeProcInstance (lpfn);
  379.         break;
  380.     }
  381.     case IDM_MASK:
  382.         fMask=!fMask;
  383.         break;
  384.     case IDM_SCREEN:
  385.         fScreen=!fScreen;
  386.         break;
  387.  
  388.     }
  389. }
  390.  
  391.  
  392. BOOL FAR PASCAL _export AboutDlgProc ( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  393. {
  394.     char ach[128];
  395.     
  396.     switch (msg)
  397.     {
  398.     case WM_INITDIALOG:
  399.         // nothing to initialize 
  400.         wsprintf(ach,"Compiled: %s %s",(LPSTR)__DATE__,(LPSTR)__TIME__);
  401.         SetDlgItemText(hwnd,IDD_TEXT,ach);
  402.         break;
  403.  
  404.     case WM_COMMAND:
  405.         switch (wParam)
  406.         {
  407.         case IDOK:
  408.         case IDCANCEL:
  409.             EndDialog(hwnd, 0);
  410.             break;
  411.  
  412.         default:
  413.             return FALSE;
  414.         }
  415.         break;
  416.  
  417.     default:
  418.         return FALSE;
  419.     }
  420.  
  421.     return TRUE;
  422. }
  423.